﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Audio;
using System.IO;
using Microsoft.Xna.Framework;

namespace MemoBoss
{
    public enum SFX
    {
        ButtonClick,
        BackButtonClick,
        CardFlip1,
        CardFlip2,
        CardFlipBack,
        CardVanish,
        RingBell,
        EndBell
    }
    public class SoundFX
    {
        public static  Dictionary<SFX, SoundEffect> SoundEffects
        {
            get;
            set;
        }
        private static List<SoundEffectInstance> SoundInstances
        {
            get;
            set;
        }
        public static void AddSound(SFX key, Stream stream)
        {
            if (SoundEffects == null)
            {
                SoundEffects = new Dictionary<SFX, SoundEffect>();
                SoundInstances = new List<SoundEffectInstance>();
            }
            if (!SoundEffects.ContainsKey(key))
            { 
                SoundEffects.Add(key, SoundEffect.FromStream(stream));
                return;
            }
            else
            {
                return;
            }
        }

        public static void initSound()
        {

            AddSound(SFX.ButtonClick, TitleContainer.OpenStream("Sound/ButtonWhip.wav"));
            AddSound(SFX.BackButtonClick, TitleContainer.OpenStream("Sound/BackButtonWhip.wav"));
            AddSound(SFX.CardFlip1, TitleContainer.OpenStream("Sound/Flip1.wav"));
            AddSound(SFX.CardFlip2, TitleContainer.OpenStream("Sound/Flip2.wav"));
            AddSound(SFX.CardFlipBack, TitleContainer.OpenStream("Sound/FlipBack.wav"));
            AddSound(SFX.CardVanish, TitleContainer.OpenStream("Sound/Vanish.wav"));
            AddSound(SFX.RingBell, TitleContainer.OpenStream("Sound/RingBell.wav"));
            AddSound(SFX.EndBell, TitleContainer.OpenStream("Sound/EndBell.wav"));
          
        }
      

        public static void PlaySoundEffect(SFX key)
        {
            PlaySoundEffect(key, true);
        }

        public static void PlaySoundEffect(SFX key, bool fireandforget)
        {
            if (App.SettingsInstance.SoundEffectsOnOffSetting)
            {
                if (SoundEffects.ContainsKey(key))
                {
                    FrameworkDispatcher.Update();
                    if (!fireandforget)
                    {
                        SoundEffectInstance soundEffectInstance = SoundEffects[key].CreateInstance();
                        soundEffectInstance.Play();
                        SoundInstances.Add(soundEffectInstance);
                        return;
                    }
                    else
                    {
                        SoundEffects[key].Play();
                        return;
                    }
                }
                else
                {
                    return;
                }
            }
            else
            {
                return;
            }
        }
     


        public void StopEffects()
        {
            if (SoundInstances == null || SoundInstances.Count == 0)
            {
                return;
            }
            else
            {
                foreach (SoundEffectInstance soundInstance in SoundInstances)
                {
                    if (soundInstance.State == SoundState.Playing)
                    {
                        soundInstance.Stop();
                    }
                    soundInstance.Dispose();
                }
                SoundInstances.Clear();
                return;
            }
        }


    }
}
